package com.google.code.netcheckin.aprs;

import com.google.code.netcheckin.util.ByteUtils;

public abstract class AprsPacketData {

	public static final int	TYPE_POSITION = 0;
	public static final int	TYPE_POSITION_MOVEMENT = 1;

	protected int		dataType;
	protected byte[]	sourceAddress;
	protected byte[]	destAddress;		// valid addresses only
	protected byte[]	path;
	protected byte		symbolTable;
	protected byte		symbolId;

	protected AprsFormatter formatter;

	public AprsPacketData() {
	}

		/**
		  * Pass a formatter that can be used to format fields. Optional. If you do not
		  * provide one, one will be instantiated when needed.
		  *
		*/

	public AprsPacketData(AprsFormatter formatter) {
		this.formatter = formatter;
	}

	public AprsPacketData(int type) {
		dataType = type;
	}

	public final byte[] getDestAddress() {
		return destAddress;
	}

	public final AprsFormatter getFormatter() {
		return formatter;
	}

	public final byte[] getPath() {
		return path;
	}

	public final byte[] getSourceAddress() {
		return sourceAddress;
	}

	public final byte getSymbolId() {
		return symbolId;
	}

	public final byte getSymbolTable() {
		return symbolTable;
	}

	public final void setDestAddress(byte[] dest) {
		destAddress = dest;
	}

		/**
		  * Pass a formatter that can be used to format fields. Optional. If you do not
		  * provide one, one will be instantiated when needed.
		  *
		*/

	public final void setFormatter(AprsFormatter formatter) {
		this.formatter = formatter;
	}

	public final void setPath(byte[] digi) {
		path = digi;
	}

	public final void setSourceAddress(byte[] src) {
		sourceAddress = src;
	}

	public final void setSymbolTable(byte table) {
		symbolTable = table;
	}

	public final void setSymbolId(byte id) {
		symbolId = id;
	}

		/** Create complete APRS packet. */

	public abstract byte[] toPacket();

	//////////////////  protected below /////////////////////////

		/**
		  * Add addressing (sender, addressee, path) to the 
		  * given buffer, begining at the given offset. 
		  *
		*/

	protected int addPacketBytes(byte[] buffer, int offset) {
		offset = ByteUtils.copyInto(buffer, offset, sourceAddress);
		buffer[offset] = AprsPacketConstants.PKT_SEP_ADDRESSES;
		offset ++;
		offset = ByteUtils.copyInto(buffer, offset, destAddress);
		buffer[offset] = AprsPacketConstants.PKT_SEP_DEST_PATH;
		offset ++;
		return ByteUtils.copyInto(buffer, offset, path);
	}

}
